package com.tchy.cloud.controller;

import com.tchy.cloud.entity.*;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 咨询师课程管理
 */
@Controller
@RequestMapping("/ccourseController")
public class ACourseController {
    private static final Logger logger = Logger.getLogger(ACourseController.class.getName());

    @Autowired
    private CommonService commonService;

    /**
     * 咨询师课程系列页面跳转
     *
     * @return
     */
    @RequestMapping(params = "seriesIndex")
    public ModelAndView indexView(HttpServletRequest request) {
        String consultantId = request.getParameter("consultantId");
        request.setAttribute("consultantId", consultantId);
        return new ModelAndView("clound/course/seriesIndex");
    }


    /**
     * 咨询师课程系列数据获取
     *
     * @param courseSeriesEntity
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "datagrid")
    public void datagrid(ACourseSeriesEntity courseSeriesEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        String consultantId = request.getParameter("consultantId");
        CriteriaQuery cq = new CriteriaQuery(ACourseSeriesEntity.class, dataGrid);
        cq.createAlias("consultantEntity", "consultantEntity");
        cq.eq("consultantEntity.consultantId", Long.valueOf(consultantId));
        cq.addOrder("id", SortDirection.desc);
        cq.add();
        // 查询条件组装
        HqlGenerateUtil.installHql(cq, courseSeriesEntity, request.getParameterMap());
        commonService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 课程系列新增或编辑页面跳转
     *
     * @param courseSeriesEntity
     * @param req
     * @return
     */
    @RequestMapping(params = "addorupdate")
    public ModelAndView addorupdateView(ACourseSeriesEntity courseSeriesEntity, HttpServletRequest req) {
        if (courseSeriesEntity.getId() != null) {
            courseSeriesEntity = commonService.getEntity(ACourseSeriesEntity.class, courseSeriesEntity.getId());
        } else {
            String consultantId = req.getParameter("consultantId");
            AConsultantEntity consultantEntity = commonService.getEntity(AConsultantEntity.class, Long.valueOf(consultantId));
            courseSeriesEntity.setConsultantEntity(consultantEntity);
        }
        req.setAttribute("entity", courseSeriesEntity);
        return new ModelAndView("clound/course/addorupdate");
    }


    /**
     * 课程系列新增及修改的数据处理
     *
     * @param courseSeriesEntity
     * @return
     */
    @RequestMapping(params = "saveorupdate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson saveorupdate(ACourseSeriesEntity courseSeriesEntity) {
        AjaxJson j = new AjaxJson();
        try {
            if (courseSeriesEntity.getId() != null) {//修改
                ACourseSeriesEntity entity = commonService.get(ACourseSeriesEntity.class, courseSeriesEntity.getId());
                MyBeanUtils.copyBeanNotNull2Bean(courseSeriesEntity, entity);
                commonService.saveOrUpdate(entity);
            } else {//保存
                commonService.save(courseSeriesEntity);
            }
            j.setMsg("操作成功");
        } catch (Exception e) {
            logger.error(e);
            j.setMsg("操作失败");
        }
        return j;
    }


    /**
     * 课程系列删除操作
     *
     * @param courseSeriesEntity
     * @return
     */
    @RequestMapping(params = "del", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson del(ACourseSeriesEntity courseSeriesEntity, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            if (courseSeriesEntity.getId() != null) {//修改
                courseSeriesEntity = commonService.get(ACourseSeriesEntity.class, courseSeriesEntity.getId());
                commonService.delete(courseSeriesEntity);
                commonService.saveOrUpdate(courseSeriesEntity);
                j.setMsg("操作成功");
            } else {
                j.setMsg("课程系列不存在，请刷新后重试。");
            }
        } catch (Exception e) {
            logger.error(e);
            j.setMsg("操作失败");
        }
        return j;
    }


    /**
     * 课程管理页面跳转
     *
     * @return
     */
    @RequestMapping(params = "courseIndex")
    public ModelAndView courseIndex() {
        return new ModelAndView("clound/course/courseIndex");
    }


    /**
     * 咨询师课程数据获取
     *
     * @param courseEntity
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "datagrid_course")
    public void datagrid_course(ACourseEntity courseEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(ACourseEntity.class, dataGrid);
        String realname = request.getParameter("consultantEntity.aCustomerEntity.realname");
        if (StringUtil.isNotEmpty(realname)) {
            courseEntity.getConsultantEntity().getaCustomerEntity().setRealname(null);
            cq.createAlias("consultantEntity", "consultantEntity");
            cq.createAlias("consultantEntity.aCustomerEntity", "aCustomerEntity");
            cq.like("aCustomerEntity.realname", "%" + realname + "%");
        }
        cq.addOrder("id", SortDirection.desc);
        cq.add();
        // 查询条件组装
        HqlGenerateUtil.installHql(cq, courseEntity, request.getParameterMap());
        commonService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }


    /**
     * 课程新增或编辑页面跳转
     *
     * @param courseEntity
     * @param req
     * @return
     */
    @RequestMapping(params = "addorupdate_course")
    public ModelAndView addorupdate_course(ACourseEntity courseEntity, HttpServletRequest req) {
        if (courseEntity.getId() != null) {
            courseEntity = commonService.getEntity(ACourseEntity.class, courseEntity.getId());
        }
        req.setAttribute("entity", courseEntity);
        return new ModelAndView("clound/course/addorupdate_course");
    }


    /**
     * 课程新增及修改的数据处理
     *
     * @param courseEntity
     * @return
     */
    @RequestMapping(params = "saveorupdate_course", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson saveorupdate_course(ACourseEntity courseEntity) {
        AjaxJson j = new AjaxJson();
        try {
            if (courseEntity.getId() != null) {//修改
                ACourseEntity entity = commonService.get(ACourseEntity.class, courseEntity.getId());
                MyBeanUtils.copyBeanNotNull2Bean(courseEntity, entity);
                commonService.saveOrUpdate(entity);
            } else {//保存
                courseEntity.setEffective(1);
                courseEntity.setDeleted(0);
                courseEntity.setPersonNum(0);
                commonService.save(courseEntity);
            }
            j.setMsg("操作成功");
        } catch (Exception e) {
            logger.error(e);
            j.setMsg("操作失败");
        }
        return j;
    }

    /**
     * 课程删除操作
     *
     * @param courseEntity
     * @return
     */
    @RequestMapping(params = "del_course", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson del_course(ACourseEntity courseEntity, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            if (courseEntity.getId() != null) {//修改
                courseEntity = commonService.get(ACourseEntity.class, courseEntity.getId());
                commonService.delete(courseEntity);
                j.setMsg("操作成功");
            } else {
                j.setMsg("课程不存在，请刷新后重试。");
            }
        } catch (Exception e) {
            logger.error(e);
            j.setMsg("操作失败");
        }
        return j;
    }

    @RequestMapping(params = "get_consultant_fl")
    @ResponseBody
//    consultant_id
    public List<Map<String, Object>> getSpecByWireId(HttpServletRequest request) {
        String consultant_id = request.getParameter("consultant_id");//咨询师id
        String sql = "select course_series_id as id,series_name as name from a_course_series where consultant_id=?";
        List<Map<String, Object>> list = commonService.findForJdbc(sql, new Object[]{Long.valueOf(consultant_id)});
        return list;
    }


    /**
     * 课程评价页面跳转
     *
     * @return
     */
    @RequestMapping(params = "courseCommentIndex")
    public ModelAndView courseCommentIndex(HttpServletRequest request) {
        String courseId = request.getParameter("courseId");//课程id
        request.setAttribute("courseId", courseId);
        return new ModelAndView("clound/course/courseCommentIndex");
    }


    /**
     * 课程评价数据获取
     *
     * @param courseCommentEntity
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "datagrid_courseComment")
    public void datagrid_courseComment(ACourseCommentEntity courseCommentEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(ACourseCommentEntity.class, dataGrid);
        String courseId = request.getParameter("courseId");
        cq.createAlias("courseOrderEntity", "courseOrderEntity");
        cq.eq("courseOrderEntity.id", Long.valueOf(courseId));

        cq.addOrder("id", SortDirection.desc);
        cq.add();
        // 查询条件组装
        HqlGenerateUtil.installHql(cq, courseCommentEntity, request.getParameterMap());
        commonService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }


    /**
     * 订单页面跳转
     *
     * @return
     */
    @RequestMapping(params = "courseOrderIndex")
    public ModelAndView courseOrderIndex(HttpServletRequest request) {
        return new ModelAndView("clound/course/courseOrderIndex");
    }

    /**
     * 订单数据数据获取
     *
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "datagrid_courseOrder")
    public void datagrid_courseOrder(ACourseOrderEntity courseOrderEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(ACourseOrderEntity.class, dataGrid);
        cq.addOrder("id", SortDirection.desc);
        cq.add();
        // 查询条件组装
        HqlGenerateUtil.installHql(cq, courseOrderEntity, request.getParameterMap());
        commonService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }

}







